package com.heima.wemedia.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdChannelDto;
import com.heima.model.wemedia.dtos.ChannelPageDto;
import com.heima.model.wemedia.entity.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.model.wemedia.entity.WmChannel;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 自媒体频道表(WmChannel)表服务实现类
 *
 * @author makejava
 * @since 2023-06-14 09:36:40
 */
@Service("wmChannelService")
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel>implements WmChannelService{
    @Resource
    private WmChannelMapper wmChannelMapper;

    @Resource
    private WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult getChannelList() {
        //封装查询条件，查询状态为1的列表
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.<WmChannel>lambdaQuery()
                .eq(WmChannel::getStatus,1)
                .orderByAsc(WmChannel::getOrd);
        List<WmChannel> channelList = wmChannelMapper.selectList(wrapper);

        return ResponseResult.okResult(channelList);
    }

    /**
     * 模糊分页查询
     * @return
     */
    @Override
    public ResponseResult pageList(ChannelPageDto dto) {

        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNotEmpty(dto.getName())){
            wrapper.like(WmChannel::getName,dto.getName());
        }
        if(dto.getPage()<=0||dto.getPage()>100){
            dto.setPage(1);
        }
        if(dto.getSize()<=0||dto.getSize()>100){
            dto.setSize(10);
        }
        wrapper.ne(WmChannel::getStatus,2);
        wrapper.orderByDesc(WmChannel::getOrd);
        Page<WmChannel> page = wmChannelMapper.selectPage(new Page<WmChannel>(dto.getPage(), dto.getSize()), wrapper);

       ResponseResult result= new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
       result.setData(page.getRecords());
       return result;
    }

    @Override
    public ResponseResult delete(Integer id) {

        //参数校验
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if(wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //查询文章表是否有频道文章,如果有则不能删除
         Integer count = wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id));
        if(count>0){
            //当前频道有文章，不能删除
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"当前频道存在文章,无法删除");
        }

        //删除数据
        wmChannel.setStatus(2);
        wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 保存频道
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveChannel(AdChannelDto dto) {
        //参数校验
        if(dto==null||StringUtils.isEmpty(dto.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        WmChannel channel = wmChannelMapper.selectOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, dto.getName()));

        if(channel!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"当前已有该频道");
        }

        //封装wmChannel
        WmChannel wmChannel = new WmChannel();
        wmChannel.setName(dto.getName());
        //wmChannel.setIsDefault(dto.getIsDefault()?1:0);
        wmChannel.setOrd(dto.getOrd());
        wmChannel.setDescription(dto.getDescription());
        wmChannel.setStatus(dto.getStatus()?1:0);

        wmChannelMapper.insert(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateChannel(AdChannelDto dto) {

        //参数校验
        if(dto==null||dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmChannel wmChannel = wmChannelMapper.selectById(dto.getId());
        if(wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        BeanUtils.copyProperties(dto,wmChannel);
        wmChannel.setIsDefault(0);
        wmChannel.setStatus(dto.getStatus()?1:0);
        wmChannel.setId(dto.getId().longValue());
        if(StringUtils.isEmpty(wmChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道名称不能为空");
        }
        wmChannelMapper.updateById(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}

